import React, { memo, useEffect, useState } from "react";

import store from "./store";

import { addAction } from "./store/actionCreators"; // 一些方法

const index = memo(() => {
  const [count, setCount] = useState(store.getState().counter);

  useEffect(() => {
    const unsubscribue = store.subscribe(() => {
      //开启订阅
      setCount(store.getState().counter);
    });
    return () => {
      unsubscribue();
    };
  }, []);
  return (
    <div>
      home
      <h2>当前计数 {count}</h2>
      <button
        onClick={(v) => {
          store.dispatch(addAction(1));
        }}
      >
        +1
      </button>
      <button
        onClick={(v) => {
          store.dispatch(addAction(5));
        }}
      >
        +5
      </button>
    </div>
  );
});

export default index;
